package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.DateCondition;
import com.itheima.entity.MonthInYear;
import com.itheima.service.ReportService;
import com.itheima.utils.DateConverterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    MemberDao memberDao;
    @Autowired
    OrderDao orderDao;

    @Override
    public Map getBusinessReportData(String dateStr) throws ParseException {
        Date date = DateConverterUtils.String2Date(dateStr, "GMT+8");
        Calendar calendar = Calendar.getInstance();
        DateCondition dateCondition = new DateCondition();
        calendar.setTime(date);
        //得到当前日期是一年中的第多少周
        int weekYear = calendar.get(Calendar.WEEK_OF_YEAR);

        //得到当前月
        int day = calendar.get(Calendar.DAY_OF_YEAR);
        int month = calendar.get(Calendar.MONTH);
        int year = calendar.get(Calendar.YEAR);
        int firstDayOfWeek = calendar.getFirstDayOfWeek();

        HashMap<String, Object> map = new HashMap<>();
        //当日数据
        dateCondition.setBeginDate(date);
        calendar.set(year,month,day+1);
        dateCondition.setEndDate(calendar.getTime());
        Integer newMember = memberDao.getNewMemberByCondition(dateCondition);
        map.put("todayNewMember",newMember);

        Integer todayOrderNumber=orderDao.getNewOrderByCondition(dateCondition);
        map.put("todayOrderNumber",todayOrderNumber);
        Integer todayVisitsNumber=orderDao.getVisitsNumberByCondition(dateCondition);
        map.put("todayVisitsNumber",todayVisitsNumber);

        //得到到这周信息
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK,2);
        dateCondition.setBeginDate(calendar.getTime());
        calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+7);
        dateCondition.setEndDate(calendar.getTime());

        Integer thisWeekNewMember=memberDao.getNewMemberByCondition(dateCondition);
        map.put("thisWeekNewMember",thisWeekNewMember);

        Integer thisWeekOrderNumber=orderDao.getNewOrderByCondition(dateCondition);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);

        Integer thisWeekVisitsNumber=orderDao.getVisitsNumberByCondition(dateCondition);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);


        //得到当月数据
        calendar.set(year,month,1);
        dateCondition.setBeginDate(calendar.getTime());
        calendar.set(year,month+1,1);
        dateCondition.setEndDate(calendar.getTime());

        Integer thisMonthNewMember=memberDao.getNewMemberByCondition(dateCondition);
        map.put("thisMonthNewMember",thisMonthNewMember);

        Integer thisMonthOrderNumber = orderDao.getNewOrderByCondition(dateCondition);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);

        Integer thisMonthVisitsNumber=orderDao.getVisitsNumberByCondition(dateCondition);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);


        //总会员数
        Integer totalMember=memberDao.getTotalMember();
        map.put("totalMember",totalMember);

        //热门套餐
        List<Map> hotSetmeal=orderDao.findHotSetmeal();
        map.put("hotSetmeal",hotSetmeal);
        map.put("reportDate",dateStr);
        return map;
    }

    @Override
    public List getMonthlyMemberCount(Date beginDate, Date endDate) {
        Calendar calendar = Calendar.getInstance();
        List<Map> list=new ArrayList<>();
        if (beginDate == null || endDate == null) {
            return null;
        }
        while (endDate.compareTo(beginDate)>=0){
            calendar.setTime(beginDate);
            String monthStart= new SimpleDateFormat("yyyy-MM").format(calendar.getTime())+"-1";
            String monthEnd= new SimpleDateFormat("yyyy-MM").format(calendar.getTime())+"-31";
            Map<String,Object> map =new HashMap<>();
            map.put("monthStart",monthStart);
            map.put("monthEnd",monthEnd);
            Integer countMember=memberDao.getMonthMember(map);
            String month= new SimpleDateFormat("yyyy-MM").format(calendar.getTime());
            Map<String,Object> count=new HashMap<>();
            count.put("month",month);
            count.put("count",countMember);
            list.add(count);
            calendar.add(Calendar.MONTH,1);
            beginDate=calendar.getTime();
        }
        return list;
    }

    @Override
    public Map queryCurrentYearMemberCount() {
        return null;
    }
}
